// service/toreItem/toreItem.js
const app = getApp()
import http from "../../utils/http"
import util from "../../utils/util"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list:[],
    finish: false,
    loading: false,
    page: 1,
    state: 0,
    safeBottom: `60px`,
    to_shop: ''
  },
  edit(e){
    let id = e.currentTarget.dataset.id
    util.skip('/service/issue/issue?id='+id)
  },
  downGoods(e){
    let id = e.currentTarget.dataset.id
    let index = e.currentTarget.dataset.index
    http.get('goods/downgoods',{id}).then(res => {
      util.toast(res.data)
      this.data.list[index].status = 'hidden'
      this.setData({
        list: this.data.list
      })
    })
  },
  upGoods(e){
    let id = e.currentTarget.dataset.id
    let index = e.currentTarget.dataset.index
    http.get('goods/upgoods',{id}).then(res => {
      util.toast(res.data)
      this.data.list[index].status = 'normal'
      this.setData({
        list: this.data.list
      })
    })
  },
  change(e){
    this.setData({
      state: e.detail.name
    })
    this.reload()
  },
  reload(){
    this.setData({
      list: [],
      page: 1,
      finish: false
    })
    this.getList()
  },
  more(){
    this.setData({
      page: ++this.data.page
    })
    this.getList()
  },
  getList(){
    if(this.data.finish){
      return 
    }
    if(this.data.loading){
      return 
    }
    this.setData({
      loading: true
    })
    http.get('shop/shopgoods', {
      shop_state: this.data.state,
      page: this.data.page,
    }).then(res => {
      if(res.data.list.length === 0){
        this.setData({
          finish: true
        })
      }
      let arr = this.data.list.concat(res.data.list)
      this.setData({
        list: arr
      })
      
    }).finally(res => {
      this.setData({
        loading: false
      })
    })
  },
  //跳转发布
  toissue(){
    wx.navigateTo({
      url: '/service/issue/issue',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const high = app.globalData.safeBottom
    this.setData({
      safeBottom: `${high}px`
    })
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.reload()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})